package com.huihe.common.mapstruct;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.mapstruct.InheritInverseConfiguration;

import java.util.*;

public interface BaseMapStruct<E,V> {

    @InheritInverseConfiguration
    V entityToVo(E entity);

    E voToEntity(V vo);

    default List<V> entityListToVoList(List<E> entityList){
        if (entityList == null){
            return Collections.emptyList();
        } else {
            List<V> list = new ArrayList<>(entityList.size());

            for (E entity : entityList) {
                list.add(this.entityToVo(entity));
            }

            return list;
        }
    }

    default List<E> voListToEntityList(List<V> voList){
        if (voList == null){
            return Collections.emptyList();
        } else {
            List<E> list = new ArrayList<>(voList.size());

            for (V vo : voList) {
                list.add(this.voToEntity(vo));
            }

            return list;
        }
    }

    default IPage<V> entityPageToVoPage(IPage<E> entityPage){
        if (entityPage.getRecords() == null){
            return null;
        } else {
            List<V> list = new ArrayList<>(entityPage.getRecords().size());

            for (E entity : entityPage.getRecords()){
                list.add(this.entityToVo(entity));
            }

            IPage<V> voPage = new Page<>();
            voPage.setRecords(list);
            voPage.setTotal(entityPage.getTotal());
            voPage.setSize(entityPage.getSize());
            voPage.setCurrent(entityPage.getCurrent());
            return voPage;
        }
    }

    default IPage<E> voPageToEntityPage(IPage<V> voPage){
        if (voPage.getRecords() == null){
            return null;
        } else {
            List<E> list = new ArrayList<>(voPage.getRecords().size());

            for (V entity : voPage.getRecords()){
                list.add(this.voToEntity(entity));
            }

            IPage<E> entityPage = new Page<>();
            entityPage.setRecords(list);
            entityPage.setTotal(voPage.getTotal());
            entityPage.setSize(voPage.getSize());
            entityPage.setCurrent(voPage.getCurrent());
            return entityPage;
        }
    }
}
